﻿using System.Linq;
using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot;
using Buddy.Swtor;
using Buddy.Swtor.Objects;
using WingIt.Dynamics;

namespace WingIt.Routines
{
    //09.05.2012 Pios
    public static class SorcererCorruption
    {
        [Behavior(BehaviorType.Pull)]
        [Class(CharacterClass.Inquisitor, AdvancedClass.Sorcerer, SkillTreeId.SorcererCorruption)]
        public static Composite SorcererCorruptionPull()
        {
            return new PrioritySelector(
                Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 29f / 10f),
                Spell.Cast("Force Lightning"),
                SorcererCorruptionCombat()
                );
        }

        [Behavior(BehaviorType.Combat)]
        [Class(CharacterClass.Inquisitor, AdvancedClass.Sorcerer, SkillTreeId.SorcererCorruption)]
        public static Composite SorcererCorruptionCombat()
        {
            return new PrioritySelector(//Generel
                Spell.Cast("Unbreakable Will", castWhen => BuddyTor.Me.IsStunned),//Insignia/2m CD
                CommonBuffsAndHealing(),
                Spell.Cast("Recklessness", castWhen => BuddyTor.Me.InCombat && BuddyTor.Me.ResourceStat >= 60),
                Spell.WaitForCast(),
                //aoe burn while companion kicks ass
                Spell.CastOnGround("Force Storm", castWhen =>
                {
                    if (Helpers.Targets.Count() > 3 && BuddyTor.Me.CurrentTarget.Distance <= 0.5f && BuddyTor.Me.ResourceStat >= 100)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                },
                    ret => BuddyTor.Me.CurrentTarget.Position
                ),
                //single target burn
                Spell.Cast("Crushing Darkness", castWhen => !BuddyTor.Me.CurrentTarget.HasDebuff("Crushing Darkness") && Helpers.Targets.Count() < 2),
                Spell.Cast("Affliction", castWhen => !BuddyTor.Me.CurrentTarget.HasDebuff("Affliction (Force)") && Helpers.Targets.Count() < 2),
                Spell.Cast("Shock", castWhen => BuddyTor.Me.ResourceStat >= 45),//Instant
                Spell.Cast("Electrocute", castWhen => BuddyTor.Me.ResourceStat >= 20),//Stun
                Spell.Cast("Force Lightning", castWhen => Helpers.Targets.Count() < 2),
                Spell.Cast("Thrash", castWhen => BuddyTor.Me.ResourceStat < 25 && BuddyTor.Me.CurrentTarget.Distance <= 0.4f),
                Spell.Cast("Saber Strike", castWhen => BuddyTor.Me.ResourceStat < 25 && BuddyTor.Me.CurrentTarget.Distance <= 0.4f),
                Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 29f / 10f));
        }

        public static Composite CommonBuffsAndHealing()
        {
            return
                new PrioritySelector(
                    Spell.Cast("Static Barrier",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      !Helpers.Companion.HasBuff("Static Barrier") &&
                                      !Helpers.Companion.HasDebuff("Deionized")),
                    Spell.Cast("Static Barrier",
                               onWhoToCast => BuddyTor.Me,
                               ret => !BuddyTor.Me.HasBuff("Static Barrier") &&
                                      !BuddyTor.Me.HasDebuff("Deionized")),
                    Spell.Cast("Resurgence",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      Helpers.Companion.HealthPercent < 90),
                    Spell.Cast("Resurgence",
                               onWhoToCast => BuddyTor.Me,
                               ret => BuddyTor.Me.HealthPercent < 90),
                    Spell.Cast("Innervate",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      Helpers.Companion.HealthPercent < 80),
                    Spell.Cast("Innervate",
                               onWhoToCast => BuddyTor.Me,
                               ret => BuddyTor.Me.HealthPercent < 80),
                    Spell.Cast("Dark Heal",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      Helpers.Companion.HealthPercent < 30),
                    Spell.Cast("Dark Heal",
                               onWhoToCast => BuddyTor.Me,
                               ret => BuddyTor.Me.HealthPercent < 30),
                    Spell.Cast("Dark Infusion",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      Helpers.Companion.HealthPercent < 25),
                    Spell.Cast("Dark Infusion",
                               onWhoToCast => BuddyTor.Me,
                               ret => BuddyTor.Me.HealthPercent < 25),
                    Spell.Cast("Consumption",
                               onWhoToCast => BuddyTor.Me,
                               ret => !BuddyTor.Me.HasDebuff("Consumption (2)") &&
                                   BuddyTor.Me.HealthPercent >= 90 &&
                                   BuddyTor.Me.ResourceStat <= 90));
        }
    }
}